package edu.lsnu.equipment_management_system.service.impl;

import edu.lsnu.equipment_management_system.mapper.update.ChangeUseStatusMapper;
import edu.lsnu.equipment_management_system.pojo.domain.UseStatusConstant;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.DateAndEquipmentNoVo;
import edu.lsnu.equipment_management_system.pojo.vo.sql_conditions.UserNoAndEquipmentNoVo;
import edu.lsnu.equipment_management_system.service.UpdateEquipmentRepairTableEndDateService;
import edu.lsnu.equipment_management_system.mapper.update.UpdateEquipmentRepairTableEndDateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author mzy
 * @Description 为修护的设备加上结束时间(即：维修完成)
 * @date 2018/11/25
 */
@Service
@Transactional(propagation= Propagation.REQUIRED)
public class UpdateEquipmentRepairTableEndDateServiceImpl implements UpdateEquipmentRepairTableEndDateService {
    @Autowired
    UpdateEquipmentRepairTableEndDateMapper updateEquipmentRepairTableEndDateMapper;
    @Autowired
    UseStatusConstant useStatusConstant;
    @Autowired
    ChangeUseStatusMapper changeUseStatusMapper;

    /**
     * @Description 为维修中的设备加上结束时间：维修中 -> 在用
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   boolean
     * @params  [beginDate, equipmentNo]
     */
    @Override
    public boolean repairToUsing(String equipmentNo, String userNo) {
        UserNoAndEquipmentNoVo userNoAndEquipmentNoVo = new UserNoAndEquipmentNoVo();
        userNoAndEquipmentNoVo.setUserNo(userNo);
        userNoAndEquipmentNoVo.setEquipmentNo(equipmentNo);
        int count =
                updateEquipmentRepairTableEndDateMapper.repairToUsing(userNoAndEquipmentNoVo);
        if (0 == count) {
            return false;
        }
        // 从待修转置回在用
        changeUseStatusMapper.changeUseStatus(equipmentNo.trim(), useStatusConstant.getUsing());
        return true;
    }
}
